package org.lql.algo.codecrush.week007;

/**
 * @author: liangqinglong
 * @date: 2025-08-18 07:45
 * @description: 704. 二分查找 <a href="https://leetcode.cn/problems/binary-search/description/">...</a>
 **/
public class Search {

	public int search(int[] nums, int target) {
		int left = 0;
		int right = nums.length - 1;
		while (left <= right) {
			int mid = (left + right) / 2;
			if (nums[mid] == target) {
				return mid;
			} else if (nums[mid] < target) {
				left = mid + 1;
			} else {
				right = mid - 1;
			}
		}
		return -1;
	}

	public static void main(String[] args) {
		Search search = new Search();
		int[] nums = {-1, 0, 3, 5, 9, 12};
		System.out.println(search.search(nums, 9));
		System.out.println(search.search(nums, 2));
	}
}
